<?php
/*********************************************************************************
 * Copyright (C) 2011-2013 X2Engine Inc. All Rights Reserved.
 *
 * X2Engine Inc.
 * P.O. Box 66752
 * Scotts Valley, California 95067 USA
 *
 * Company website: http://www.x2engine.com
 * Community and support website: http://www.x2community.com
 *
 * X2Engine Inc. grants you a perpetual, non-exclusive, non-transferable license
 * to install and use this Software for your internal business purposes.
 * You shall not modify, distribute, license or sublicense the Software.
 * Title, ownership, and all intellectual property rights in the Software belong
 * exclusively to X2Engine.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT.
 ********************************************************************************/

/* @edition:pro */

class GetRoleAccessAction extends CAction{
    public function run(){
        if (isset($_POST['Roles']) || isset($_GET['name'])) {
            if(isset($_POST['Roles']))
                $name = $_POST['Roles']['name'];
            else
                $name = $_GET['name'];
            if (is_null($name)) {
                echo "";
                exit;
            }
            $auth=Yii::app()->authManager;
            $modules=Modules::model()->findAll();
            $authItems=array();
            $tempAuthItems=$auth->getAuthItems(1);
            foreach($tempAuthItems as $item){
                $authItems[]=$item->name;
            }
            $accessGroups=array();
            $selected=array();
            foreach($modules as $module){
                $accessGroups[$module->name][]="-";
                $items=$this->controller->Array_Search_Preg("^".ucfirst($module->name)."(.*?)Access", $authItems);
                foreach($items as $item){
                    $tempName=$authItems[$item];
                    if($auth->hasItemChild($name,$tempName)){
                        $selected[$module->name]=$authItems[$item];
                    }
                    $authItems[$item]=preg_replace('/'.ucfirst($module->name).'/','',$authItems[$item]);
                    $accessGroups[$module->name][$tempName]=Yii::t('admin',Formatter::deCamelCase($authItems[$item]));
                }
            }
            foreach($accessGroups as $module=>$accessGroup){
                echo "<div class='row'>";
                echo Yii::t('admin',"Permissions for module:")." <b>".Yii::t('app',ucfirst($module))."</b> ".CHtml::dropDownList($module.'Access',isset($selected[$module])?$selected[$module]:'-',$accessGroup);
                echo "</div>";
            }
        }
    }
}

?>
